import { defineStore } from 'pinia';

export const useProductStore = defineStore('product', {
  state: () => ({
    categories: [
      { 
        id: 1, 
        name: '饼干',
        subCategories: [
          { id: 101, name: '夹心饼干' },
          { id: 102, name: '曲奇饼干' },
          { id: 103, name: '威化饼干' },
          { id: 104, name: '苏打饼干' },
          { id: 105, name: '巧克力饼干' },
          { id: 106, name: '燕麦饼干' },
        ]
      },
      { 
        id: 2, 
        name: '糖果',
        subCategories: [
          { id: 201, name: '硬糖' },
          { id: 202, name: '软糖' },
          { id: 203, name: '棒棒糖' },
          { id: 204, name: '果冻糖' },
          { id: 205, name: '奶糖' },
          { id: 206, name: '巧克力糖' },
        ]
      },
      {
        id: 3,
        name: '薯片',
        subCategories: [
          { id: 301, name: '原味薯片' },
          { id: 302, name: '烧烤味薯片' },
          { id: 303, name: '番茄味薯片' },
          { id: 304, name: '芥末味薯片' },
          { id: 305, name: '海苔味薯' },
          { id: 306, name: '酸奶味薯片' },
        ]
      },
      {
        id: 4,
        name: '坚果',
        subCategories: [
          { id: 401, name: '杏仁' },
          { id: 402, name: '腰果' },
          { id: 403, name: '花生' },
          { id: 404, name: '开心果' },
          { id: 405, name: '核桃' },
          { id: 406, name: '松子' },
        ]
      },
      {
        id: 5,
        name: '巧克力',
        subCategories: [
          { id: 501, name: '牛奶巧克力' },
          { id: 502, name: '黑巧克力' },
          { id: 503, name: '白巧克力' },
          { id: 504, name: '夹心巧克力' },
          { id: 505, name: '榛子巧克力' },
          { id: 506, name: '薄荷巧克力' },
        ]
      },
      {
        id: 6,
        name: '果干',
        subCategories: [
          { id: 601, name: '葡萄干' },
          { id: 602, name: '蔓越莓干' },
          { id: 603, name: '芒果干' },
          { id: 604, name: '香蕉片' },
          { id: 605, name: '苹果干' },
          { id: 606, name: '桃子干' },
        ]
      },
      {
        id: 7,
        name: '软糖',
        subCategories: [
          { id: 701, name: '水果软糖' },
          { id: 702, name: '棉花糖' },
          { id: 703, name: '果冻软糖' },
          { id: 704, name: '夹心软糖' },
          { id: 705, name: '酸味软糖' },
          { id: 706, name: '巧克力软糖' },
        ]
      },
      {
        id: 8,
        name: '膨化食品',
        subCategories: [
          { id: 801, name: '爆米花' },
          { id: 802, name: '玉米片' },
          { id: 803, name: '虾条' },
          { id: 804, name: '锅巴' },
          { id: 805, name: '麻花' },
          { id: 806, name: '米果' },
        ]
      },
      {
        id: 9,
        name: '饮料',
        subCategories: [
          { id: 901, name: '碳酸饮料' },
          { id: 902, name: '果汁' },
          { id: 903, name: '茶饮料' },
          { id: 904, name: '咖啡' },
          { id: 905, name: '能量饮料' },
          { id: 906, name: '矿泉水' },
        ]
      },
      {
        id: 10,
        name: '零食礼盒',
        subCategories: [
          { id: 1001, name: '综合零食礼盒' },
          { id: 1002, name: '坚果礼盒' },
          { id: 1003, name: '巧克力礼盒' },
          { id: 1004, name: '糖果礼盒' },
          { id: 1005, name: '饼干礼盒' },
          { id: 1006, name: '节日特别礼盒' },
        ]
      },
    ],
    products: [
      { 
        id: 1, 
        name: '夹心饼干', 
        price: 9.99, 
        image: '/images/bg.jpg',
        categoryId: 101,
        description: '美味的夹心饼干',
        specialOffer: true
      },
      { 
        id: 2, 
        name: '曲奇饼干', 
        price: 12.99, 
        image: '/images/bg.jpg',
        categoryId: 102,
        description: '香脆可口的曲奇',
        specialOffer: true
      },
      { 
        id: 3, 
        name: '硬糖', 
        price: 5.99, 
        image: '/images/bg.jpg',
        categoryId: 201,
        description: '经典硬糖',
        specialOffer: true
      },
      { 
        id: 4, 
        name: '软糖', 
        price: 6.99, 
        image: '/images/bg.jpg',
        categoryId: 202,
        description: '美味软糖',
        specialOffer: true
      },
      {
        id: 5,
        name: '威化饼干',
        price: 8.99,
        image: '/images/bg.jpg',
        categoryId: 103,
        description: '层层美味的威化饼干',
        specialOffer: true
      },
      {
        id: 6,
        name: '苏打饼干',
        price: 7.99,
        image: '/images/bg.jpg',
        categoryId: 104,
        description: '健康美味的苏打饼干',
        specialOffer: true
      },
      {
        id: 7,
        name: '水果硬糖',
        price: 5.99,
        image: '/images/bg.jpg',
        categoryId: 201,
        description: '多种水果口味硬糖',
        specialOffer: true
      },
      {
        id: 8,
        name: '果味软糖',
        price: 6.99,
        image: '/images/bg.jpg',
        categoryId: 202,
        description: 'Q弹美味的果味软糖',
        specialOffer: true
      },
      {
        id: 9,
        name: '原味薯片',
        price: 8.99,
        image: '/images/bg.jpg',
        categoryId: 301,
        description: '经典原味薯片',
        specialOffer: true
      },
      {
        id: 10,
        name: '烧烤味薯片',
        price: 8.99,
        image: '/images/bg.jpg',
        categoryId: 302,
        description: '美味烧烤味薯片',
        specialOffer: true
      },
      {
        id: 11,
        name: '原味杏仁',
        price: 29.99,
        image: '/images/bg.jpg',
        categoryId: 401,
        description: '优质杏仁',
        specialOffer: true
      },
      {
        id: 12,
        name: '盐焗腰果',
        price: 32.99,
        image: '/images/bg.jpg',
        categoryId: 402,
        description: '香脆可口的腰果',
        specialOffer: true
      },
      {
        id: 13,
        name: '牛奶巧克力',
        price: 15.99,
        image: '/images/bg.jpg',
        categoryId: 501,
        description: '香浓牛奶巧克力',
        specialOffer: true
      },
      {
        id: 14,
        name: '黑巧克力',
        price: 18.99,
        image: '/images/bg.jpg',
        categoryId: 502,
        description: '醇厚黑巧克力',
        specialOffer: true
      },
      {
        id: 15,
        name: '葡萄干',
        price: 12.99,
        image: '/images/bg.jpg',
        categoryId: 601,
        description: '新疆葡萄干',
        specialOffer: true
      },
      {
        id: 16,
        name: '蔓越莓干',
        price: 16.99,
        image: '/images/bg.jpg',
        categoryId: 602,
        description: '蔓越莓干',
        specialOffer: true
      },
      // ... 为每个分类添加至少2-3个商品
    ],
    cartItems: []
  }),
  getters: {
    getProductById: (state) => (id) => {
      return state.products.find((product) => product.id === id);
    },
    
    cartTotal() {
      return this.cartItems.reduce((total, item) => total + item.price * item.quantity, 0);
    },
    specialOfferProducts(state) {
      return state.products.map(product => ({
        ...product,
        specialOffer: product.price < 10
      }));
    }
  },
  actions: {
    getCategoryProducts(categoryId) {
      return this.products.filter(p => p.categoryId === categoryId);
    },
    findCategory(id) {
      return this.categories.find(c => c.subCategories.some(sub => sub.id === id));
    },
    getCategoryName(categoryId) {
      const category = this.findCategory(categoryId);
      if (!category) return '未知分类';
      
      const subCategory = category.subCategories.find(sub => sub.id === categoryId);
      return subCategory ? subCategory.name : category.name;
    },
    addToCart(product) {
      const existingItem = this.cartItems.find(item => item.id === product.id);
      if (existingItem) {
        existingItem.quantity++;
      } else {
        this.cartItems.push({ ...product, quantity: 1 });
      }
    },
    removeFromCart(productId) {
      const index = this.cartItems.findIndex(item => item.id === productId);
      if (index !== -1) {
        this.cartItems.splice(index, 1);
      }
    },
    updateCartItemQuantity(productId, quantity) {
      const item = this.cartItems.find(item => item.id === productId);
      if (item) {
        item.quantity = quantity;
      }
    },
    getCartItemQuantity(productId) {
      const item = this.cartItems.find(item => item.id === productId);
      return item ? item.quantity : 0;
    }
  }
});
